package com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.service;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.util.Log;

import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.Utils.Constant;
import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.Utils.LogUtil;
import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.Utils.UserLocation;
import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.activity.HomePHPActivity;
import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.callback.LocationCallBack;
import com.baidu.location.BDLocation;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2017/9/14.
 */

public class UserLocationService  extends Service implements LocationCallBack {


    private UserLocation userLocation = UserLocation.getUserLocationInstance();
    private String username;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        username = intent.getStringExtra("username");
        //18696515408
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                userLocation.QueryEntityLocation(getApplicationContext(), username,UserLocationService.this);
                // userLocation.getUserLocation(HomePHPActivity.this,HomePHPActivity.this);
                handler.postDelayed(this,60*1000);
            }
        }, 10*1000);

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void getUserLocation(BDLocation location) {

    }

    @Override
    public void getUserCoordinate(double latitude, double longitude) {
         //获取用户坐标位置 回调
        JSONObject SendJS = new JSONObject();
        try {
            SendJS.put("username",username);
            SendJS.put("latitude",String.valueOf(latitude));
            SendJS.put("longitude",String.valueOf(longitude));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        SendMessageToJs(SendJS.toString());
    }

    private void SendMessageToJs(String jsonObject){

        LogUtil.i("TRAIL","---发送到服务器的数据-----"+jsonObject);
        HttpParams httpParams = new HttpParams();//.put("",jsonObject);

        httpParams.put("params",jsonObject);
        OkGo.<String>post(Constant.LOCATION_URL+username).params(httpParams).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {


            }
        });
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
